package com.example.erp.config.mybatisPlus;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * MP 插入填充
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //有这个属性执行 没有不执行
        boolean hasSetter = metaObject.hasSetter("createTime");
        if(hasSetter){
            setFieldValByName("createTime",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"),metaObject);

        }
//        strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //是否自己设置值
        boolean hasSetter = metaObject.hasSetter("updateTime");
        if(hasSetter){
            Object val = getFieldValByName("updateTime", metaObject);
            if(val==null){
                strictUpdateFill(metaObject,"updateTime", String.class, DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));

            }
        }
    }

}
